package com.elink.enginerring.file;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.ws.rs.DefaultValue;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elink.enginerring.category.entity.DetailCategory;
import com.elink.enginerring.category.entity.SecondaryCategory;
import com.elink.enginerring.category.entity.TopCategory;
import com.elink.enginerring.category.service.ICategoryService;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.util.JSONUtil;

@Controller
@RequestMapping("category")
public class FileCategoryController {
	@Autowired
	private ICategoryService categoryService;

	/**
	 * 跳转二级分类列表页
	 * @return
	 */
	@RequestMapping("second.htm")
	public String toSecond(@Param("tcId")String tcId,HttpServletRequest request){
		System.out.println("一级分类"+tcId);
		request.setAttribute("tcId", tcId);
		return "category/second";
	}

	/**
	 * 跳转三级分类列表页
	 * @param scId
	 * @param tcId
	 * @param request
	 * @return
	 */
	@RequestMapping(value={"third.htm","third1.htm","third2.htm"})
	public String toThird1(@Param("scId")String scId,@Param("tcId")String tcId,HttpServletRequest request){
		request.setAttribute("scId", scId);
		request.setAttribute("tcId", tcId);
		StringBuffer url = request.getRequestURL();
		return "category/"+url.substring(url.lastIndexOf("/")+1,url.length()-4);
	}
	
	
	/**
	 * 跳转到详情分类
	 * @param scId
	 * @param request
	 * @return
	 */
	@RequestMapping("detail.htm")
	public String toDetail(
			@Param("scId")String scId,@Param("tcId")String tcId,
			@Param("dcTypeId")String dcTypeId,HttpServletRequest request){
		System.out.println("二级分类"+scId);
		request.setAttribute("scId", scId);
		request.setAttribute("tcId", tcId);
		request.setAttribute("dcTypeId", dcTypeId);
		return "category/detail";
	}

	/**
	 * 获取资料库一级分类
	 * @return
	 */
	@RequestMapping(value = "/getTopCategoryOfDatabase", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTopCategoryOfDatabase() {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return JSONUtil.getJSON(ErrorType.SUCCESS,categoryService.getTopCategoryOfDatabase());
	}
	
	/**
	 * 获取职业考证一级分类
	 * @return
	 */
	@RequestMapping(value = "/getTopCategoryOfCert", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTopCategoryOfCert() {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return JSONUtil.getJSON(ErrorType.SUCCESS,categoryService.getTopCategoryOfCert());
	}
	
	/**
	 * 获取建筑类别一级分类
	 * @return
	 */
	@RequestMapping(value = "/getTopCategoryOfBuild", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTopCategoryOfBuild() {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return JSONUtil.getJSON(ErrorType.SUCCESS,categoryService.getTopCategoryOfBuild());
	}
	
	/**
	 * 获取工程技能一级分类
	 * @return
	 */
	@RequestMapping(value = "/getTopCategoryOfSkill", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getTopCategoryOfSkill() {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return JSONUtil.getJSON(ErrorType.SUCCESS,categoryService.getTopCategoryOfSkill());
	}
	
	/**
	 * 获取二级分类接口
	 * @param tcId
	 * @return
	 */
	@RequestMapping(value = "/getSecondaryCategory", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getSecondaryCategory(
			@RequestParam("tcId") int tcId) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return JSONUtil.getJSON(ErrorType.SUCCESS,categoryService.getSecondaryCategory(tcId));
	}

	/**
	 * 获取详情分类接口
	 * @param scId
	 * @return
	 */
	@RequestMapping(value = "/getDetailCategory", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getDetailCategory(
			@RequestParam("scId") int scId,
			@RequestParam(value="dcTypeId",required=false)@DefaultValue("0") int dcTypeId) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return JSONUtil.getJSON(ErrorType.SUCCESS,categoryService.getDetailCategory(scId,dcTypeId));
	}
	
	
	/**
	 * 添加一级分类
	 * @param tc
	 * @return
	 */
	@RequestMapping(value = "/addTop", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addTop(@ModelAttribute @Valid TopCategory tc) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		categoryService.addTop(tc);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 修改一级分类
	 * @param tc
	 * @return
	 */
	@RequestMapping(value = "/modifyTop", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyTop(@ModelAttribute @Valid TopCategory tc) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		categoryService.modifyTop(tc);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 添加二级分类
	 * @param sc
	 * @return
	 */
	@RequestMapping(value = "/addSecond", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addSecond(@ModelAttribute @Valid SecondaryCategory sc) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		categoryService.addSecond(sc);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 修改二级分类
	 * @param sc
	 * @return
	 */
	@RequestMapping(value = "/modifySecond", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifySecond(@ModelAttribute @Valid SecondaryCategory sc) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		categoryService.modifySecond(sc);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	/**
	 * 添加三级分类
	 * @param dc
	 * @return
	 */
	@RequestMapping(value = "/addDetail", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addDetail(@ModelAttribute @Valid DetailCategory dc) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		categoryService.addDetail(dc);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 修改详情分类
	 * @param dc
	 * @return
	 */
	@RequestMapping(value = "/modifyDetail", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyDetail(@ModelAttribute @Valid DetailCategory dc) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		categoryService.modifyDetail(dc);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 删除分类
	 * @param id
	 * @param type 1-- 一级分类  2-- 二级分类 3-- 详情分类
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> del(@RequestParam("id")int id,@RequestParam("type")int type) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		categoryService.del(id,type);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
}
